package ml.uuku.swipeheadertabs.adapter;

import java.util.ArrayList;
import java.util.List;

import ml.uuku.swipeheadertabs.ui.fragment.base.BaseFragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

/**
 * 为ViewPager添加Fragment的适配器
 * 
 * @author <a href=http://my.oschina.net/uuku>uuku</a>
 * @version V0.1
 */
public class ViewPagerFragmentAdapter extends FragmentStatePagerAdapter {
    private final List<String> tabTitle;
    private final List<BaseFragment> pagerItems;

    public ViewPagerFragmentAdapter(FragmentManager fm) {
        super(fm);
        tabTitle = new ArrayList<String>();
        pagerItems = new ArrayList<BaseFragment>();
    }

    public void addFragment(BaseFragment fragment, String title) {
        pagerItems.add(fragment);
        tabTitle.add(title);
        notifyDataSetChanged();
    }

    @Override
    public BaseFragment getItem(int position) {
        return pagerItems.get(position);
    }

    @Override
    public int getCount() {
        return pagerItems.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitle.get(position);
    }
}
